package com.cy.aircompressor.main.home.systemsetting.detail;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;

import com.cy.aircompressor.config.AirConfig;
import com.cy.aircompressor.databinding.DialogAddOrUpdateUserInfoBinding;
import com.cy.aircompressor.databinding.DialogPowerStationAddOrUpdateBinding;
import com.cy.aircompressor.entity.power.PowerStation;
import com.cy.aircompressor.entity.select.SelectDict;
import com.cy.aircompressor.entity.system.UserInfo;
import com.cy.aircompressor.main.home.systemsetting.viewmodel.SystemSettingViewModel;
import com.cy.aircompressor.main.view.pop.AirDropdownPopupWindow;
import com.drug.lib.util.ToastUtil;
import com.jakewharton.rxbinding2.view.RxView;
import com.net.model.base.Resource;
import com.net.model.base.fragment.BaseVMDialogFragment;
import com.net.model.livedatas.LiveDataBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class PowerStationAddOrUpdateDialog extends BaseVMDialogFragment<DialogPowerStationAddOrUpdateBinding, SystemSettingViewModel> {
    boolean isEdit;
    PowerStation powerStation;

    public static void showDialogFragment(FragmentManager fragmentManager, String tag, boolean isEdit, PowerStation powerStation) {
        Bundle args = new Bundle();
        PowerStationAddOrUpdateDialog fragment = new PowerStationAddOrUpdateDialog();
        args.putSerializable("powerStation", powerStation);
        args.putBoolean("isEdit", isEdit);
        fragment.setArguments(args);
        fragment.show(fragmentManager, tag);
    }

    @Override
    protected void dataChange() {
        mViewModel.getAddPowerStation().observe(this, new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> objectResource) {
                objectResource.handler(new OnCallback<Object>(true, true) {
                    @Override
                    public void onSuccess(Object data) {
                        ToastUtil.show("新增成功");
                        LiveDataBus.get().with("power_station", String.class).post("add");
                        dismiss();
                    }
                });
            }
        });
        mViewModel.getUpdatePowerStation().observe(this, new Observer<Resource<Object>>() {
            @Override
            public void onChanged(Resource<Object> objectResource) {
                objectResource.handler(new OnCallback<Object>(true, true) {
                    @Override
                    public void onSuccess(Object data) {
                        LiveDataBus.get().with("power_station", String.class).post("update");
                        ToastUtil.show("更新成功");
                        dismiss();
                    }
                });
            }
        });
    }

    @Override
    protected DialogPowerStationAddOrUpdateBinding getViewBinding() {
        return DialogPowerStationAddOrUpdateBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        isEdit = getArguments().getBoolean("isEdit");
        powerStation = (PowerStation) getArguments().getSerializable("powerStation");

        if (powerStation != null) {
            setUserInfoDetail();
            setUserInfoEdit();
        } else {
            mBinding.btnSave.setTag("add");
            mBinding.btnSave.setText("保存");
            mBinding.titleName.setText("新增电站");
        }
        switchView(isEdit);


        RxView.clicks(mBinding.closeBtn)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    dismiss();
                });

        RxView.clicks(mBinding.btnSave)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (TextUtils.equals((CharSequence) mBinding.btnSave.getTag(), "add")) {
                        Object object = getParameters((String) mBinding.btnSave.getTag());
                        mViewModel.addPowerStation(object);
                    } else if (TextUtils.equals((CharSequence) mBinding.btnSave.getTag(), "update")) {
                        Object object = getParameters((String) mBinding.btnSave.getTag());
                        mViewModel.updatePowerStation(object);
                    } else if (TextUtils.equals((CharSequence) mBinding.btnSave.getTag(), "detail")) {
                        isEdit = true;
                        setUserInfoEdit();
                        switchView(isEdit);
                    }
                });

    }

    @Override
    protected void requestNetwork() {

    }

    private Map getParameters(String tag) {
        Map<String, String> jsonObject = new HashMap<>();
        String powerName = mBinding.etPowerName.getText().toString().trim();
        String remark = mBinding.etRemark.getText().toString().trim();
        if (TextUtils.isEmpty(powerName)) {
            ToastUtil.show("请完善电站信息！");
            return null;
        }
        jsonObject.put("name", powerName);
        jsonObject.put("remark", remark);
        if (TextUtils.equals(tag, "update")) {
            jsonObject.put("id", powerStation.getId());
        }
        return jsonObject;
    }

    private void switchView(boolean isEdit) {
        mBinding.addUpdateUserInfo.setVisibility(isEdit ? View.VISIBLE : View.GONE);
        mBinding.userInfoDetail.setVisibility(isEdit ? View.GONE : View.VISIBLE);
    }

    private void setUserInfoDetail() {
        if (powerStation == null) return;
        if (isEdit) return;
        mBinding.powerName.setText(powerStation.getName());
        mBinding.createTime.setText(powerStation.getCreateTime());
        mBinding.updateTime.setText(powerStation.getUpdateTime());
        mBinding.remark.setText(powerStation.getRemark());

        mBinding.btnSave.setTag("detail");
        mBinding.btnSave.setText("编辑");
        mBinding.titleName.setText("电站详情");

    }

    private void setUserInfoEdit() {
        if (powerStation == null) return;
        if (!isEdit) return;
        mBinding.etPowerName.setText(powerStation.getName());
        mBinding.etRemark.setText(powerStation.getRemark());
        mBinding.btnSave.setTag("update");
        mBinding.btnSave.setText("保存");
        mBinding.titleName.setText("编辑电站");
    }


}
